package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.manager.mapper.GoodsTblMapper;
import com.amigo.online.provider.manager.model.entity.GoodsTbl;
import com.amigo.online.provider.manager.service.GoodsTblService;
import com.amigo.online.provider.manager.util.param.dto.RedisKey;

@Service
public class GoodsTblServiceImpl implements GoodsTblService {

    @SuppressWarnings("rawtypes")
    @Autowired
    RedisTemplate redisTemplate;

    @Resource
    private GoodsTblMapper goodsTblMapper;

    @Override
    public List<GoodsTbl> findGoodsTblList(GoodsTbl goodsTbl) {

        return goodsTblMapper.findGoodsTblList(goodsTbl);
    }

    @Override
    public boolean updateGoodsTbl(GoodsTbl goodsTbl) {
        goodsTbl.setUpdatedTime(System.currentTimeMillis() / 1000);

        if (goodsTbl.getStatus() != null) {
            if (goodsTbl.getStatus() == 2) {
                @SuppressWarnings("unchecked")
                Long redisResult = redisTemplate.opsForSet().add(RedisKey.MERCHANT_GOODS_SET, goodsTbl.getId());
                if (redisResult == 0) {
                    // logger.error("下架商家商品缓存数据有误，操作员ID:{},商家ID:{},商品ID:{}", userId, merchantId,
                    // userId);
                }

                goodsTbl.setUpTime(System.currentTimeMillis() / 1000);

            } else if (goodsTbl.getStatus() == 3) {
                @SuppressWarnings("unchecked")
                Long redisResult = redisTemplate.opsForSet().remove(RedisKey.MERCHANT_GOODS_SET, goodsTbl.getId());
                if (redisResult == 0) {
                    // logger.error("上架商家商品缓存数据有误，操作员ID:{},商家ID:{},商品ID:{}", userId, merchantId,
                    // userId);
                }
                goodsTbl.setDownTime(System.currentTimeMillis() / 1000);
            }

        }

        return goodsTblMapper.updateGoodsTbl(goodsTbl) > 0;
    }

}
